import * as constants from './constants';
import { fromJS } from 'immutable';
import axios from 'axios';

const homeList = (data) => ({
  type: constants.GET_LIST,
  data: fromJS(data),
});

const addHomeList = (list, nextPage) => ({
  type: constants.ADD_ARTICLE_LIST,
  list: fromJS(list),
  nextPage
});

export const toggleTopShow = (show) => ({
  type: constants.TOGGLE_SCROLL_TOP,
  show
});

export const homeData = () => {
  return (dispatch) => {
    axios.get('/api/home.json').then((res) => {
      const data = res.data;
      dispatch(homeList(data.data));
    }).catch(() => {
      console.log('error');
    });
  };
};

export const getMoreList = (page) => {
  return (dispatch) => {
    axios.get('/api/homeList.json?page=' + page).then((res) => {
      dispatch(addHomeList(res.data.data, page + 1));
    }).catch(() => {
      console.log('error');
    });
  };
};